package joctave.toolbox.io;

import joctave.core.tokens.*;
import joctave.core.tokens.numbertokens.DoubleNumberToken;
import joctave.core.tokens.numbertokens.Int32NumberToken;
import joctave.core.functions.ExternalFunction;
import joctave.core.interpreter.GlobalValues;
import joctave.toolbox.joctave.video.ImageStack;

import java.io.*;

/**An external function for getting a directory listing         */
public class read extends ExternalFunction
{
	public OperandToken evaluate(Token[] operands, GlobalValues globals)
	{

        if (getNArgIn(operands) != 2)
			throwMathLibException("read: number of arguments != 2");

		if (!(operands[0] instanceof MathLibObject)) 
            throwMathLibException("read: argument must be a variable");
		if (!(operands[1] instanceof NumberToken)) 
            throwMathLibException("read: argument must be a number");

		MathLibObject vid=((MathLibObject)operands[0]);
		ImageStack stack=(ImageStack)vid.getData();
		int[] frame=(int[]) stack.getPixels((int)((DoubleNumberToken)operands[1]).getValueRe());
		int[][]frames=new int[1][];
		frames[0]=frame;
		return new Int32NumberToken(frames);
		//System.out.println("vid"+vid.getFieldData("Video"));
        /*String name = ((CharToken)operands[0]).toString();

        File file = null;
        
		try
		{
			file = new File(globals.getWorkingDirectory(),name);
		}
		catch (Exception e)
		{
            throwMathLibException("delete: exception");
		}		    
        
		if (!file.delete())
                throwMathLibException("delete: did not work");*/

		//return null;		

	} // end eval
}

/*
@GROUP
IO
@SYNTAX
dir()
dir(directory)
@DOC
Displays the file list for the current directory.
@EXAMPLES
<programlisting>
dir()
MathLib.MathLib.html
MathLib.log
MathLib.bat
object.txt
Demos.class
MathLib/
CVS/
Demos$1.class
1.0
</programlisting>
@SEE
cd, createnewfile, exist, mkdir, rmdir, delete, isfile, isdirectory, ishidden, lastmodified
*/

